//
//  ViewController.m
//  PushDemo
//
//  Created by chenliang on 2018/7/31.
//  Copyright © 2018年 chenl. All rights reserved.
//

#import "ViewController.h"
#import "CLInterceptor.h"
#import "EnvConfig.h"
#import "ActivityIndicatorView.h"

@interface ViewController ()<UIWebViewDelegate>

@end

@implementation ViewController{
    UIWebView *_webView;
    CLInterceptor *_interceptor;
    ActivityIndicatorView *_activity;
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    _interceptor = [[CLInterceptor alloc]init];
    
    _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    _webView.backgroundColor = [UIColor whiteColor];
    _webView.delegate = self;
    [self.view addSubview:_webView];
    
    _activity = [[ActivityIndicatorView alloc]initWithFrame:_webView.bounds];
    [_webView addSubview:_activity];
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadUrl];
}
-(NSString *)getPath:(NSString *)url{
    NSString *path = nil;
    if([url hasPrefix:@"http"]){
        path = url;
    }else{
        NSString *rp = [[NSBundle mainBundle] pathForResource:Index_Load_Url ofType:nil];
        if (rp) {
            path = rp;
        }
    }
    return path;
}

-(void)loadUrl{
    if(_webView){
        NSString *path = [self getPath:Index_Load_Url];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]];
    }
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *url = [request.URL absoluteString];
    if([_interceptor isPluginUrl:url]){
        [_interceptor filter:url webView:webView webViewController:self];
        return NO;
    }
    
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [_activity startAnimat];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [_activity stopAnimat];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [_activity stopAnimat];
}

- (void)dealloc{
    
}
@end
